<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\ZhuceInsertRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use App\User;

class adminController extends Controller
{
    //配置首页路由, 此文件请勿删除
    public function inde(Request $request)
    {
    	// $users = User::();
    	$id = session('id');
           	$name = DB::table('users')
           	// ->join('user_detail','users.id','=','user_detail.id')
           	->where('id','=',$id)
           	->select('userName')
           	->first();
    	$data = $request->session()->all();
        return view('admins.index',['users'=>$name]);
    }
    //后台登录界面
    public function getLogin()
    {
    	return view('admins.login');
    }
    //后台登录首页验证
    public function postDologin(Request $request)
    {
        $info = DB::table('users')->where('userName',$request->input('userName'))->where('auth',1)->where('status',1)->value('password');
    	$user = DB::table('users')->where('userName',$request->input('userName'))->first();
        if(Hash::check($request->input('password'),$info)) {
           	session(['id' => $user['id']]);
           	
            return redirect('/admin');
        
        } else {
            return back();
        }
    }
    //后台注册页面
    public function getAdd()
    {
    	return view('admins.admin-add');
    }
    //后台注册验证
    public function postUseradd(ZhuceInsertRequest $request)
    {
    	$user = new User;
        $userName = $user -> userName = $request->userName;
        $password = $user -> password = Hash::make($request->input('password'));
        $auth = $user -> auth = $request->auth;

            $res = DB::table('users')->insert([
                    'userName'=>$userName,
                    'password'=>$password,
                    'auth'=>$auth,
                    'lastLogin'=> date('Y-m-d H:i:s',time())

                ]);
            $id = DB::table('users')->where('userName','=',"$userName")->first()['id'];
            $ress = DB::table('user_detail')->insert([
                    'nickName'=>$userName,
                    'id'=>$id,
                    'email'=>'1234@qq.com'

                ]);
        if($res && $ress) {
            return redirect('/user/userlist');
        } else {
            return back();
        }
        

    }
    //后台用户列表页面
    public function getUserlist(Request $request)
    {
        $keyword = $request -> input('keyword');
        // if(empty($keyword)){
        //      $users = User::orderBy('id','desc')
        //     ->paginate(5);
        // } else {
        //      $users = User::orderBy('id','desc')
        //     ->paginate(5)
        //     ->where('userName','like','%.$keyword.%');
        // }
        $users = User::orderBy('id','desc')
            ->where(function($query)use($keyword){
                if(!empty($keyword)){
                    $query->where('userName','like','%'.$keyword.'%');
                }
            })
            ->paginate(5)
            ;
       
        $auth = array('1'=>'超级管理员', '0'=>'普通用户');
        $status = array('1'=>'已启用', '0'=>'已禁用');
        
        
    	return view('admins.admin-list',['users'=>$users,'auth'=>$auth,'status'=>$status]);
    }
    //后台修改密码
    public function xiugai($id)
    {
    	$users = User::findOrFail($id);
    	return view('/admins.admin-xiugai',['users'=>$users]);
    }
    //密码验证
    public function doxiugai(UpdateUserRequest $request,$id)
    {
    	
    	$users = User::findOrFail($id);
    	$pass = $users->password = Hash::make($request->password);
        $res = DB::table('users')->where('id',$id)->update(['password'=>$pass]);
        if($res){
        	return redirect('/user/userlist');
        }else {
        	return back();
        }
    	
     }
     //删除用户
     public function del($id)
     {
        $users = User::findOrFail($id);
        if($users->delete()) {
            return redirect('/user/userlist');
        } else {
            return back();
        }
     }
     //修改状态
     public function status($id)
     {
        $users = User::findOrFail($id);
        $sta = $users ->status;
        if($sta == 1) {
            $sta = 0;
        } else {
            $sta = 1;
        }
        $users->status = $sta;
        $res = DB::table('users')->where('id',$id)->update(['status'=>$sta]);
        if($res){
        	return redirect('/user/userlist');
        }else {
        	return back();
        }
     }

}
